﻿

#region Using Statements

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

#endregion


namespace Freeform.Framework.Graphics
{
    /// <summary>
    /// An actor that renders itself as a text line.
    /// </summary>
    public class Actor_Text2D : Actor
    {

        #region Members

        /// <summary>
        /// Gets or sets the text for this actor.
        /// </summary>
        public virtual String Text { get; set; }


        /// <summary>
        /// Gets or sets the spritefont of this actor.
        /// </summary>
        public virtual SpriteFont Font { get; set; }


        /// <summary>
        /// Gets or sets whether the actor should use the natural font size to draw.
        /// If true the font size will be scaled by the Entity's NaturalFontScaling value (use Vector2.One for the natural font size.)
        /// Otherwise the size of the font will be resized to be exactly equal to the Entity's size value.
        /// </summary>
        public virtual Boolean UseNaturalFontSize { get; set; }


        /// <summary>
        /// Gets or sets the value to scale the natural font size by if the actor is using the natural font size.
        /// </summary>
        public virtual Vector2 NaturalFontScaling { get; set; }

        #endregion


        #region Init

        /// <summary>
        /// Creates a new 2d text actor.
        /// </summary>
        /// <param name="entity">The entity that this actor represents.</param>
        public Actor_Text2D(Entity entity)
            : base(entity)
        {
            Text = "";
            Font = null;
            NaturalFontScaling = Vector2.One;
        }


        /// <summary>
        /// Creates a new 2d text actor.
        /// </summary>
        /// <param name="entity">The entity that this actor represents.</param>
        /// <param name="text">The text of the actor.</param>
        /// <param name="font">The font of the actor.</param>
        public Actor_Text2D(Entity entity, String text, SpriteFont font)
            : base(entity)
        {
            Text = text;
            Font = font;
            NaturalFontScaling = Vector2.One;
        }

        #endregion


        #region Draw

        /// <summary>
        /// Draws the entity onto the viewport.
        /// </summary>
        /// <param name="spatial">The spatial properties of the actor in world coordinates.</param>
        /// <param name="viewport">The viewport onto which the entity is being rendered.</param>
        /// <param name="time">Provides a snapshot of timing values.</param>
        public override void Draw(Viewport2D viewport, GameTime time)
        {
            // Check early breaks.
            if (viewport.SpriteBatch == null) return;
            if (Font == null) return;
            if (Text == null) return;
            if (Entity == null) return;

            Vector2 Draw_Position = viewport.Camera.TransformPosition(Entity.SpatialProperties.Position);
            Vector2 Draw_Measure = Font.MeasureString(Text);
            Vector2 Draw_Origin = Draw_Measure / 2;

            // If we're using natural font size we need to adjust the size of the entity.
            if (UseNaturalFontSize)
                Entity.SpatialProperties.Size = Draw_Measure * NaturalFontScaling;

            Vector2 Draw_Size = viewport.Camera.TransformSize(Entity.SpatialProperties.Size);
            Vector2 Draw_Scale = Draw_Size / Draw_Measure;

            viewport.SpriteBatch.DrawString(Font, Text, Draw_Position, ColourOverlay, Entity.SpatialProperties.Direction, Draw_Origin, Draw_Scale, SpriteEffects.None, Depth);
        }

        #endregion

    }
}